package com.baiytfp.hf.HFDataExchange.action.dao;

import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.baiytfp.framework.base.BaseDao;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class AreaDAOImpl extends BaseDao {
	/**
	 * 根据 tree_id 获取对应 的 tree_code 的子节点
	 * @param treeType
	 * @param tree_id
	 * @return
	 * @throws Exception
	 */
	public List<Map> getTreeList(String treeType, String tree_id) throws Exception {
		StringBuffer sWhere = new StringBuffer("where 1 = 1 ");
		List params = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append("select t.* from ").append(TableName.TREE).append(" t ");

		if (sWhere.length() > 0) {
			sWhere.append(" and t.tree_type = ? ");
			params.add(treeType);
		}

		if (!"".equals(tree_id)) {
			sWhere.append(" and t.parent_tree_code = (select tree_code from "+TableName.TREE+" where id = ? ) ");
			params.add(tree_id);
		}

		String orderBy = " order by node_deep asc, parent_tree_code, node_order asc "; // 先把父结点取出来

		List<Map> list = HFDBHelper.querySql(sql + sWhere.toString() + orderBy, Map.class, params.toArray());

		return list;
	}
	/**
	 * 根据 地区树 顶级节点的 tree_code 获取子节点
	 * @param treeType
	 * @param toptree_code
	 * @return
	 * @throws Exception
	 */
	public List<Map> getAreaList(String treeType, String toptree_code) throws Exception {
		StringBuffer sql=new StringBuffer();
		List params = new ArrayList();
		sql.append(" select t.id, t.node_name from "+TableName.TREE+" t ")
				.append(" where 1 = 1 ");

		if (!"".equals(treeType) && !"".equals(toptree_code)) {
			sql.append(" and t.tree_type = ? ");
			params.add(treeType);
			sql.append(" and t.parent_tree_code = ? ");
			params.add(toptree_code);
		}
		List<Map> list = HFDBHelper.querySql(sql.toString(), Map.class, params.toArray());

		return list;
	}
}
